package com.boxin.ims.modules.wechat.busi;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.entity.ticket.Ticket;
import com.boxin.ims.modules.wechat.entity.ticket.TicketConfig;
import com.boxin.ims.modules.wechat.service.ticket.TicketConfigService;
import com.boxin.ims.modules.wechat.service.ticket.TicketService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * @author Jakemanse
 * @date 2014-1-28
 * @function <p> 入场券处理 </p>
 */
@Service
public class TicketProcessor extends BusinessProcessor{
	
	@Autowired
	private TicketService ticketService;
	
	

	@Override
	public ProcessMessage process() {
		
		WechatMember member = memberCheckAndReply();
		
		if(member != null){
			Ticket ticketQuery = new Ticket();
			ticketQuery.setUser(user);
			ticketQuery.setMember(member);
			List<Ticket> ticketList   = ticketService.find(ticketQuery);
			
			TicketConfig ticketConfig = WechatUtils.getTicketConfigByUserId(user.getId());
			
			String title ="入场券";
			String name = "入场券";
			String desc = "";
			if(ticketConfig!=null){
				if(StringUtils.isNotEmpty(ticketConfig.getName())){
					name = ticketConfig.getName();
				}
				
				if(StringUtils.isNotEmpty(ticketConfig.getTitle())){
					title = ticketConfig.getTitle();
				}
				
				if(StringUtils.isNotEmpty(ticketConfig.getQrcodeDesc())){
					desc = ticketConfig.getQrcodeDesc();
				}
				
				
				//已经猎取过入场券
				if(ticketList!=null && !ticketList.isEmpty()){
					Ticket ticket = ticketList.get(0);
					Long qrcodeId = ticket.getQrCode().getId();
					writeImageToUser(title, desc, qrcodeId);
					return null;
				}else{
					
					Ticket ticket = new Ticket();
					ticket.setUser(user);
					ticket.setMember(member);
					ticket.setStatus(Ticket.STATUS_NORMAL);
					ticket.setName(name);
					
					ticketService.save(ticket);
					Long qrcodeId = ticket.getQrCode().getId();
					writeImageToUser(title, desc, qrcodeId);
					return null;
				}
				
				
			}else{
				writeToUser("对不起,该服务还未启动。");
			}
			
		}
		
		
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		return false;
	}

}
